home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / ripley / source / getbits.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  5KB  |  221 lines

  1. /* getbits.c, bit level routines                                            */
  2.  
  3. /*
  4.  * All modifications (mpeg2decode -> mpeg2play) are
  5.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  6.  */
  7.  
  8. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  9.  
  10. /*
  11.  * Disclaimer of Warranty
  12.  *
  13.  * These software programs are available to the user without any license fee or
  14.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  15.  * any and all warranties, whether express, implied, or statuary, including any
  16.  * implied warranties or merchantability or of fitness for a particular
  17.  * purpose.  In no event shall the copyright-holder be liable for any
  18.  * incidental, punitive, or consequential damages of any kind whatsoever
  19.  * arising from the use of these programs.
  20.  *
  21.  * This disclaimer of warranty extends to the user of these programs and user's
  22.  * customers, employees, agents, transferees, successors, and assigns.
  23.  *
  24.  * The MPEG Software Simulation Group does not represent or warrant that the
  25.  * programs furnished hereunder are free of infringement of any third-party
  26.  * patents.
  27.  *
  28.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  29.  * are subject to royalty fees to patent holders.  Many of these patents are
  30.  * general enough such that they are unavoidable regardless of implementation
  31.  * design.
  32.  *
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <fcntl.h>  
  38.  
  39. #include "config.h"
  40. #include "global.h"
  41.  
  42. #include "mplay.h"
  43.  
  44. #ifndef __PPC__
  45. #include <proto/asyncio.h>
  46. #endif
  47.  
  48. /* initialize buffer, call once before first getbits or showbits */
  49.  
  50. void Initialize_Buffer()
  51. {
  52.   ld->Incnt = 0;
  53.   ld->Rdptr = ld->Rdbfr + 2048;
  54.   ld->Rdmax = ld->Rdptr;
  55.  
  56. #ifdef VERIFY
  57.   /*  only the verifier uses this particular bit counter 
  58.    *  Bitcnt keeps track of the current parser position with respect
  59.    *  to the video elementary stream being decoded, regardless 
  60.    *  of whether or not it is wrapped within a systems layer stream 
  61.    */
  62.   ld->Bitcnt = 0;
  63. #endif
  64.  
  65.   ld->Bfr = 0;
  66.   Flush_Buffer(0); /* fills valid data into bfr */
  67. }
  68.  
  69. void Fill_Buffer()
  70. {
  71.   int Buffer_Level;
  72.  
  73. #ifdef __PPC__
  74.         Buffer_Level = PPCRead(ld->Infile,ld->Rdbfr,2048);
  75. #else
  76.         Buffer_Level = ReadAsync(AsyncInfile, ld->Rdbfr, 2048);
  77. #endif
  78.  
  79.   ld->Rdptr = ld->Rdbfr;
  80.  
  81.   if (System_Stream_Flag)
  82.     ld->Rdmax -= 2048;
  83.  
  84.   
  85.   /* end of the bitstream file */
  86.   if (Buffer_Level < 2048)
  87.   {
  88.     /* just to be safe */
  89.     if (Buffer_Level < 0)
  90.       Buffer_Level = 0;
  91.  
  92.     /* pad until the next to the next 32-bit word boundary */
  93.     while (Buffer_Level & 3)
  94.       ld->Rdbfr[Buffer_Level++] = 0;
  95.  
  96.     /* pad the buffer with sequence end codes */
  97.     while (Buffer_Level < 2048)
  98.     {
  99.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
  100.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
  101.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
  102.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
  103.     }
  104.   }
  105. }
  106.  
  107.  
  108. /* MPEG-1 system layer demultiplexer */
  109.  
  110. int Get_Byte()
  111. {
  112.   while(ld->Rdptr >= ld->Rdbfr+2048)
  113.   {
  114.  
  115. #ifdef __PPC__
  116.         PPCRead(ld->Infile,ld->Rdbfr,2048);
  117. #else
  118.         ReadAsync(AsyncInfile, ld->Rdbfr, 2048);
  119. #endif
  120.  
  121.     ld->Rdptr -= 2048;
  122.     ld->Rdmax -= 2048;
  123.   }
  124.   return *ld->Rdptr++;
  125. }
  126.  
  127. /* extract a 16-bit word from the bitstream buffer */
  128. int __inline Get_Word()
  129. {
  130.   int Val;
  131.  
  132.   Val = Get_Byte();
  133.   return (Val<<8) | Get_Byte();
  134. }
  135.  
  136.  
  137. /* return next n bits (right adjusted) without advancing */
  138.  
  139. unsigned int __inline Show_Bits(N)
  140. int N;
  141. {
  142.   return ld->Bfr >> (32-N);
  143. }
  144.  
  145.  
  146. /* return next bit (could be made faster than Get_Bits(1)) */
  147.  
  148. unsigned int __inline Get_Bits1()
  149. {
  150.   return Get_Bits(1);
  151. }
  152.  
  153.  
  154. /* advance by n bits */
  155.  
  156. void Flush_Buffer(N)
  157. int N;
  158. {
  159.   int Incnt;
  160.  
  161.   ld->Bfr <<= N;
  162.  
  163.   Incnt = ld->Incnt -= N;
  164.  
  165.   if (Incnt <= 24)
  166.   {
  167.     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  168.     {
  169.       do
  170.       {
  171.         if (ld->Rdptr >= ld->Rdmax)
  172.           Next_Packet();
  173.         ld->Bfr |= Get_Byte() << (24 - Incnt);
  174.         Incnt += 8;
  175.       }
  176.       while (Incnt <= 24);
  177.     }
  178.     else if (ld->Rdptr < ld->Rdbfr+2044)
  179.     {
  180.       do
  181.       {
  182.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  183.         Incnt += 8;
  184.       }
  185.       while (Incnt <= 24);
  186.     }
  187.     else
  188.     {
  189.       do
  190.       {
  191.         if (ld->Rdptr >= ld->Rdbfr+2048)
  192.           Fill_Buffer();
  193.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  194.         Incnt += 8;
  195.       }
  196.       while (Incnt <= 24);
  197.     }
  198.     ld->Incnt = Incnt;
  199.   }
  200.  
  201. #ifdef VERIFY 
  202.   ld->Bitcnt += N;
  203. #endif /* VERIFY */
  204.  
  205. }
  206.  
  207.  
  208. /* return next n bits (right adjusted) */
  209.  
  210. unsigned int Get_Bits(N)
  211. int N;
  212. {
  213.   unsigned int Val;
  214.  
  215.   Val = Show_Bits(N);
  216.   Flush_Buffer(N);
  217.  
  218.   return Val;
  219. }
  220.  
  221.